package com.driverstation;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class MainActivity extends ActionBarActivity
{
    ControllerView controllerView;

    public Controller[] controller = new Controller[2];

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        controller[0] = new Controller();
        controller[1] = new Controller();
        controllerView = new ControllerView(this);
        controllerView.RegisterController(controller[0], 0);
        controllerView.RegisterController(controller[1], 1);
        setContentView(controllerView);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(controller[0].assignmentNeeded)
        {
            controller[0].id = event.getDeviceId();
            controller[0].assignmentNeeded = false;
        }
        else if(controller[1].assignmentNeeded)
        {
            controller[1].id = event.getDeviceId();
            controller[1].assignmentNeeded = false;
        }

        int controllerNumber = (event.getDeviceId() == controller[0].id) ? 0 : 1;

        //Set button variables if any buttons are pressed
        if(keyCode == KeyEvent.KEYCODE_BUTTON_A)controller[controllerNumber].btnA = true;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_B)controller[controllerNumber].btnB = true;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_X)controller[controllerNumber].btnX = true;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_Y)controller[controllerNumber].btnY = true;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_START)controller[controllerNumber].btnStart = true;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_SELECT)controller[controllerNumber].btnSelect = true;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_L1)controller[controllerNumber].btnLeftBumper = true;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_R1)controller[controllerNumber].btnRightBumper = true;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_THUMBL)controller[controllerNumber].leftStickClick = true;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_THUMBR)controller[controllerNumber].rightStickClick = true;

        controllerView.invalidate();

        return true;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
        int controllerNumber = (event.getDeviceId() == controller[0].id) ? 0 : 1;

        //Set button variables if any buttons are pressed
        if(keyCode == KeyEvent.KEYCODE_BUTTON_A)controller[controllerNumber].btnA = false;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_B)controller[controllerNumber].btnB = false;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_X)controller[controllerNumber].btnX = false;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_Y)controller[controllerNumber].btnY = false;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_START)controller[controllerNumber].btnStart = false;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_SELECT)controller[controllerNumber].btnSelect = false;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_L1)controller[controllerNumber].btnLeftBumper = false;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_R1)controller[controllerNumber].btnRightBumper = false;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_THUMBL)controller[controllerNumber].leftStickClick = false;
        if(keyCode == KeyEvent.KEYCODE_BUTTON_THUMBR)controller[controllerNumber].rightStickClick = false;

        controllerView.invalidate();

        return true;
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent event)
    {
        int controllerNumber = (event.getDeviceId() == controller[0].id) ? 0 : 1;

        //Set joystick values
        controller[controllerNumber].joyLeftX = event.getAxisValue(MotionEvent.AXIS_X);
        controller[controllerNumber].joyLeftY = event.getAxisValue(MotionEvent.AXIS_Y);
        controller[controllerNumber].joyRightX = event.getAxisValue(MotionEvent.AXIS_Z);
        controller[controllerNumber].joyRightY = event.getAxisValue(MotionEvent.AXIS_RZ);
        //Set top hat values
        controller[controllerNumber].topHatUp = event.getAxisValue(MotionEvent.AXIS_HAT_Y) == -1;
        controller[controllerNumber].topHatDown = event.getAxisValue(MotionEvent.AXIS_HAT_Y) == 1;
        controller[controllerNumber].topHatLeft = event.getAxisValue(MotionEvent.AXIS_HAT_X) == -1;
        controller[controllerNumber].topHatRight = event.getAxisValue(MotionEvent.AXIS_HAT_X) == 1;
        //Set trigger values
        controller[controllerNumber].btnLeftTrigger = event.getAxisValue(MotionEvent.AXIS_BRAKE) != 0;
        controller[controllerNumber].btnRightTrigger = event.getAxisValue(MotionEvent.AXIS_GAS) != 0;

        controllerView.invalidate();

        return true;
    }
}
